﻿package dt 
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import game.events.EventData;
	import game.constants.ConstData;
	import game.exchangeddata.HashTable;
	import game.GlobalVariables;
	
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)
	 */
	public class SwapRoomPanel extends MovieClip
	{
		public var txtChangeRoomTf:TextField;
		public var btnChangeRoom:SimpleButton;
		public var btnBac1:SimpleButton;
		public var btnBac2:SimpleButton;
		public var btnBac3:SimpleButton;
		public var btnBac4:SimpleButton;
		public var btnBac5:SimpleButton;
		public var btnBac6:SimpleButton;
		public var btnBac7:SimpleButton;
		public var btnBac8:SimpleButton;
		public var activeBac:MovieClip;
		
		public var bgBac1:MovieClip;
		public var bgBac2:MovieClip;
		public var bgBac3:MovieClip;
		public var bgBac4:MovieClip;
		public var bgBac5:MovieClip;
		public var bgBac6:MovieClip;
		public var bgBac7:MovieClip;
		public var bgBac8:MovieClip;
		
		public var nameBtnActive:String;
		private var arrRoomBac:Array;
		private var arrBtn:Array;
		private var arrBgBtn:Array;
		
		public function SwapRoomPanel() 
		{
			hide();
			this.txtChangeRoomTf.mouseEnabled = false;
			this.btnChangeRoom.mouseEnabled = false;
			//this.arrRoomBac = new Array(1,4,7,13,14,15,16);
			this.arrBtn = new Array(btnChangeRoom, btnBac1, btnBac2, btnBac3, btnBac4, btnBac5, btnBac6, btnBac7, btnBac8);
			this.arrBgBtn = new Array(bgBac1, bgBac2, bgBac3, bgBac4, bgBac5, bgBac6, bgBac7, bgBac8);
			var leng:int = arrBtn.length;
			for (var i:int = 0; i < leng; i++ )
			{
				this.arrBtn[i].addEventListener(MouseEvent.CLICK, hdlBtnClick);
			}
			
			this.arrRoomBac = new Array( -1, -1, -1, -1, -1, -1, -1, -1);
			for (var j:int = 0; j < GlobalVariables.arrListGameBacTable.length; j++) {
				if(GlobalVariables.arrListGameBacStatus[j] != -1) 
					this.arrRoomBac[j] = GlobalVariables.arrListGameBacTable[j];
			}
			setCloseTable();
		}
		
		
		/**
		 * @author	Bamboo
		 * @param	txtChangeRoom
		 */
		public function initText(hashTableInfo:HashTable):void {
			this.txtChangeRoomTf.text = hashTableInfo.lookupString("MNchangeTable");
		}
		/**
		 * @author Bamboo
		 * @param	e
		 */
		private function hdlBtnClick(e:MouseEvent):void
		{
			
			var event:EventData; 
			var room:String = searchRoom(nameBtnActive);
			
			if (e.currentTarget != btnChangeRoom)
			{
				event = new EventData(EventData.GET_HISTORY, true);
				searchBtn(nameBtnActive).mouseEnabled = true;
				room =  e.currentTarget.name.substr(ConstData.LEN_NAME_ROOM_BAC, e.currentTarget.name.length);
				
				event.activeRoomBac = arrRoomBac[int(room) -1];
				updatePos(SimpleButton(e.currentTarget));
				this.btnChangeRoom.mouseEnabled = true;
				this.nameBtnActive = e.currentTarget.name;
			}
			else
			{				
				event = new EventData(EventData.CHANGE_ROOM, true);
				event.activeRoomBac = room;
				this.nameBtnActive = null;
				//hide();// do it for 3 bac
			}
			//e.currentTarget.mouseEnabled = false;
			dispatchEvent(event);
		}
		/**
		 * @author	Bamoo
		 * @param	room
		 */
		public function setPosActiveRoom(room:int):void {
			//Do not more one
			for (var i:int = 0; i < this.arrRoomBac.length; i++)
			{
				if (room == arrRoomBac[i])
				{	
					updatePos(this.arrBtn[i + 1]);
					this.nameBtnActive = this.arrBtn[i + 1].name;
					this.arrBtn[i + 1].mouseEnabled = false;
					break;
				}
			}
		}
		/**
		 * @author	Bamboo
		 * @param	btn
		 */
		private function updatePos(btn:SimpleButton):void {
			this.activeBac.y = btn.y;
			this.activeBac.x = btn.x;
			this.activeBac.mouseEnabled = false;
		}
		/**
		 * @author	Bamboo
		 * @param	nameBtn
		 * @return
		 */
		private function searchBtn(nameBtn:String):SimpleButton {
			var btnActive:SimpleButton;
			for (var i:int = 0; i <this.arrBtn.length; i++)
			{
				if (nameBtn == this.arrBtn[i].name)
				{	
					btnActive = this.arrBtn[i];
					break;
				}
			}
			
			return btnActive;
		}
		/**
		 * @author	Bamboo
		 * @return
		 */
		public function checkActiveRoom():Boolean {
			if (this.nameBtnActive != null)
				return true;
			return false;	
			
		}
		/**
		 * @author	Bamboo
		 * @param	nameBtn
		 * @return
		 */
		private function searchRoom(nameBtn:String):String {
			var room:String = "";
			for (var i:int = 0; i < this.arrBtn.length; i++)
			{
				if (nameBtn == this.arrBtn[i].name)
				{	
					room = this.arrRoomBac[i - 1];
					break;
				}
			}
			return room;
		}
		
		public function hide():void {
			this.visible = false;
		}
		public function show():void {
			this.visible = true;
		}
		
		
		public function setEnabled(listRoom:Array):void {
			var n:int = 0;
			var m:int = 0;
			var i:int = 0;
			var j:int
			for (i = n; i < listRoom.length; i++ )
				for (j = m; j < this.arrRoomBac.length; j++)
				if (this.arrRoomBac[j] == listRoom[i])
					{
						this.arrBtn[j + 1].mouseEnabled = false;
						this.arrBgBtn[j].gotoAndStop("lock");
						i++;
						if (i >= listRoom.length)
							return;
					}
		}
		
		private function setCloseTable():void {
			for (var i:int = 0; i < this.arrRoomBac.length; i++) {
				if (this.arrRoomBac[i] == -1) {
					this.arrBtn[i+1].mouseEnabled = false;
					this.arrBgBtn[i].gotoAndStop("lock");
				}
			}
		}
	}

}